package com.android.drm;

import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;

import com.android.drm.to.Action;
import com.android.drm.to.User;

public class HomeScreen_orig extends Activity implements
		AdapterView.OnItemSelectedListener {

	private Button mGoButton;
	private Button mHelpButton;
	private Button mExitButton;
	private Spinner mUserName;
	private Spinner mActivity;

	private List<User> userList;
	private List<Action> actionList;
	private DRMApplication drmApplication;

	private long userId;
	private String userName;
	private long actionId;
	private String actionName;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.home_screen_orig);
		drmApplication = (DRMApplication) this.getApplication();

		setWidgetReferences();
		addUsernameAdapter();
		addActivityAdapter();
	}

	public void onItemSelected(AdapterView<?> parent, View v, int position,
			long id) {
		if (parent.getId() == findViewById(R.id.userName).getId()) {
			userName = (String) mUserName.getSelectedItem();
			userId = position == 0 ? 0 : userList.get(position - 1).getId();
		} else {
			actionName = (String) mActivity.getSelectedItem();
			actionId = position == 0 ? 0 : actionList.get(position - 1).getId();
		}
	}

	public void onNothingSelected(AdapterView<?> parent) {
		// Don't have anything to do.
	}

	public void myClickHandler(View v) {
		switch (v.getId()) {
		case R.id.goButton:
			doActivity();
			break;
		case R.id.addUserButton:
			Intent intent = new Intent(this, ManageUser.class);
			startActivity(intent);
			break;
		case R.id.helpButton:
			new AlertDialog.Builder(HomeScreen_orig.this).setMessage(
					R.string.HomeHelpMsg).setNeutralButton("Close", null)
					.show();
			break;
		case R.id.exitButton:
			finish();
			break;
		default:
			break;
		}
	}

	private void setWidgetReferences() {
		mGoButton = (Button) this.findViewById(R.id.goButton);
		mHelpButton = (Button) this.findViewById(R.id.helpButton);
		mExitButton = (Button) this.findViewById(R.id.exitButton);
		mUserName = (Spinner) this.findViewById(R.id.userName);
		mActivity = (Spinner) this.findViewById(R.id.activity);
	}

	private void addUsernameAdapter() {
		mUserName.setOnItemSelectedListener(this);
		ArrayAdapter<String> aa = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, getUserNames());
		aa
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		mUserName.setAdapter(aa);
	}

	private void addActivityAdapter() {
		mActivity.setOnItemSelectedListener(this);
		ArrayAdapter<String> aa2 = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, getActionNames());
		aa2
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		mActivity.setAdapter(aa2);
	}

	private void doActivity() {
		if (userId == 0 && actionId == 0) {
			Toast.makeText(this, "Please select a user and an activity!",
					Toast.LENGTH_LONG).show();
			return;
		} else if (userId == 0) {
			Toast.makeText(this, "Please select a user!", Toast.LENGTH_LONG)
					.show();
			return;
		} else if (actionId == 0) {
			Toast.makeText(this, "Please select an activity!",
					Toast.LENGTH_LONG).show();
			return;
		}
		Intent intent = null;
		switch (new Long(actionId).intValue()) {
		case 1:
			// "Test 4 of 9 was selected";
			intent = new Intent(this, FourOfNine.class);
			break;
		case 2:
			intent = new Intent(this, ChartSelection.class);
			break;
		default:
			break;
		}

		if (intent != null) {
			setIntentExtras(intent);
			startActivity(intent);
		}
	}

	private void setIntentExtras(Intent intent) {
		intent.putExtra("userId", userId);
		intent.putExtra("actionId", actionId);
		intent.putExtra("userName", userName);
		intent.putExtra("actionName", actionName);
	}

	private String[] getUserNames() {
		userList = HomeScreen_orig.this.drmApplication.getDataHelper().selectUsers();
		String[] userNames = new String[userList.size() + 1];
		userNames[0] = getString(R.string.select_user);
		for (int i = 0; i < userList.size(); i++) {
			userNames[i + 1] = userList.get(i).getUserName();
		}
		return userNames;
	}

	private String[] getActionNames() {
		actionList = this.drmApplication.getDataHelper()
				.selectActions();
		String[] actions = new String[actionList.size() + 1];
		actions[0] = getString(R.string.select_activity);
		for (int i = 0; i < actionList.size(); i++) {
			actions[i + 1] = actionList.get(i).getName();
		}
		return actions;
	}
}